package ystp.service;


import jxl.SheetSettings;
import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.*;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import ystp.dao.ChuangxinchanpinjieguoMapper;
import ystp.model.Chuangxinchanpinjieguo;
import ystp.model.ChuangxinchanpinjieguoExample;
import ystp.model.Ysuser;
import ystp.model.YuecaikangjunyaowujieguoExample;
import ystp.support.StringHandle;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@Service
public class ChuangxinchanpinjieguoService {

    @Resource
    ChuangxinchanpinjieguoMapper ddMapper;

    @Resource
    ChuangxinchanpinService ddChuangxinchanpinService;

    @Resource
    YsuserService ddYsuserService;

    public Chuangxinchanpinjieguo findOne(String ppId){
        return ddMapper.selectByPrimaryKey(ppId);
    }

    public Integer getChuangxinchanpinResultCount(String mmxiangmuId,String mmuserId,String mmresult){
        Object mmCount = ddMapper.getChuangxinchanpinResultCount(mmxiangmuId,mmuserId,mmresult);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }

    public boolean add(Chuangxinchanpinjieguo ppChuangxinchanpinjieguo){
        return ddMapper.insert(ppChuangxinchanpinjieguo)==1;
    }
    public boolean modify(Chuangxinchanpinjieguo ppChuangxinchanpinjieguo){
        return ddMapper.updateByPrimaryKeySelective(ppChuangxinchanpinjieguo)==1;
    }

    public Chuangxinchanpinjieguo checkExistByUserId(String ppId,String ppUserId){
        ChuangxinchanpinjieguoExample mmExample = new ChuangxinchanpinjieguoExample();
        mmExample.createCriteria().andDetailidEqualTo(ppId).andUseridEqualTo(ppUserId);
        return ddMapper.selectByExample(mmExample).size()>0?ddMapper.selectByExample(mmExample).get(0):null;
    }

    public int countByDetailId(String ppDetailId,String ppResult){
        ChuangxinchanpinjieguoExample mmExample = new ChuangxinchanpinjieguoExample();
        mmExample.createCriteria().andDetailidEqualTo(ppDetailId).andResultEqualTo("1");
        return ddMapper.countByExample(mmExample);
    }

    public void daochuqianzibiao(@RequestParam("xiangmuid") String ppxiangmuid,
                                 @RequestParam("userid") String ppuserid,
                                 HttpServletRequest request, HttpServletResponse response){

        response.reset();
        response.setContentType("application/x-download;charset=utf-8");
        List<Map<String,Object>> mmList = ddChuangxinchanpinService.findChuangxinBiaojueByXiangmuId(ppxiangmuid,ppuserid,1,999);
        Ysuser mmuser=ddYsuserService.findOne(ppuserid);
        String mmusername=mmuser.getUsername();
        OutputStream os=null;
        WritableWorkbook wbook=null;
        try
        {
            os = new BufferedOutputStream(response.getOutputStream());//得到输出流的语句
            //response.getOutputStream();// 取得输出流
            response.reset();// 清空输出流
            SimpleDateFormat mmDateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss");
            String wenjiantitle = "南京市生物医药创新产品目录"+mmusername+"委员结果表"+ StringHandle.tiemString(); //文件名
            String tmptitle = "东部战区总医院南京市生物医药创新产品目录投票表（"+mmusername+"）"; // 标题
            response.setHeader("Content-disposition", "attachment; filename="+new String(wenjiantitle.getBytes(),"iso8859-1")+".xls");// 设定输出文件头
            response.setContentType("application/vnd.ms-excel");// 定义输出类型
            wbook = Workbook.createWorkbook(os); // 建立excel文件
            WritableSheet wsheet = wbook.createSheet(tmptitle, 0); // sheet名称
            SheetSettings settings=wsheet.getSettings();
            settings.setProtected(true);
            // 设置excel标题
            WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16,WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); //设置表格样式

            WritableCellFormat wcfFC = new WritableCellFormat(wfont);
            wcfFC.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN); // 添加边框
            wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC.setAlignment(Alignment.CENTRE);

            WritableCellFormat wcfFC3 = new WritableCellFormat(wfont);
            wcfFC3.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC3.setAlignment(Alignment.CENTRE);
            //wcfFC.setBackground(Colour.AQUA);
            Label label=new Label(0, 0, tmptitle, wcfFC3);

            WritableCellFormat cellFormat=new WritableCellFormat();
            cellFormat.setAlignment(jxl.format.Alignment.CENTRE);
            cellFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
            cellFormat.setWrap(true);
            //cellFormat.setBackground(Colour.AQUA);
            cellFormat.setFont(wfont);
            label.setCellFormat(cellFormat);
            wsheet.addCell(label);
            wsheet.addCell(new Label(0, 0, tmptitle, wcfFC3));
            /*wsheet.addCell(new Label(7, 0, tmptitle, wcfFC));*/
            wsheet.setRowView(0,600); //第一行高度
            wsheet.setRowView(1,600); //第二行高度
            wsheet.mergeCells(0, 0, 9, 0);  //合并单元格(第一列的第一行和第十三列的第一行合并)
            wsheet.setColumnView(0,5); //宽度
            wsheet.setColumnView(1,22); //宽度
            wsheet.setColumnView(2,10); //宽度
            wsheet.setColumnView(3,10); //宽度
            wsheet.setColumnView(4,10); //宽度
            wsheet.setColumnView(5,20); //宽度
            wsheet.setColumnView(6,25); //宽度
            wsheet.setColumnView(7,10); //宽度
            wsheet.setColumnView(8,10); //宽度
            wsheet.setColumnView(9,10); //宽度

            // 开始生成主体内容
            wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 12,WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
            wcfFC = new WritableCellFormat(wfont);
            wcfFC.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
            wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC.setAlignment(Alignment.CENTRE);
            wcfFC.setWrap(true);
            // 不需要边框和居中的样式
            WritableCellFormat wcfFC2 = new WritableCellFormat(wfont);
            wcfFC2 = new WritableCellFormat(wfont);
            wcfFC2.setWrap(true);
            //  在  1 列  2 行 写入内容
            wsheet.addCell(new Label(0, 1, "投票人(签字):投票内容已核对无误。"+mmusername,wcfFC2));
            // 把  0 列 2 行 到 9 列 2 行合并
            wsheet.mergeCells(0, 1, 6, 1);


            wsheet.addCell(new Label(7, 1, "投票日期："+StringHandle.shorttiemString(),wcfFC2));
            wsheet.mergeCells(7, 1, 9, 1);


            wsheet.addCell(new Label(0, 2, "序号",wcfFC));
            wsheet.mergeCells(0, 2, 0, 3);

            wsheet.addCell(new Label(1, 2, "药品名称",wcfFC));
            wsheet.mergeCells(1, 2, 1, 3);

            wsheet.addCell(new Label(2, 2, "规格",wcfFC));
            wsheet.mergeCells(2, 2, 2, 3);

            wsheet.addCell(new Label(3, 2, "单位",wcfFC));
            wsheet.mergeCells(3, 2, 3, 3);

            wsheet.addCell(new Label(4, 2, "价格（元）",wcfFC));
            wsheet.mergeCells(4, 2, 4, 3);

            wsheet.addCell(new Label(5, 2, "生产厂家",wcfFC));
            wsheet.mergeCells(5, 2, 5, 3);

            wsheet.addCell(new Label(6, 2, "执行时间",wcfFC));
            wsheet.mergeCells(6, 2, 6, 3);

            wsheet.addCell(new Label(7, 2, "完成金额",wcfFC));
            wsheet.mergeCells(7, 2, 8, 2);

            wsheet.addCell(new Label(7, 3, "院本部",wcfFC));

            wsheet.addCell(new Label(8, 3, "秦淮",wcfFC));

            wsheet.addCell(new Label(9, 2, "表决意见",wcfFC));
            wsheet.mergeCells(9, 2, 9, 3);

            // 表头结束

            wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 12,WritableFont.NO_BOLD,false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
            wcfFC = new WritableCellFormat(wfont);
            wcfFC.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
            wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC.setAlignment(Alignment.CENTRE);
            wcfFC.setWrap(true);

            int count=mmList.size();
            int mmKaishihang=4;

            for (int i = 0; i < mmList.size(); i++) {
                Map<String,Object> mmMap = mmList.get(i);
                if (mmMap!=null){
                    String mmXuhao = mmMap.get("ordernumber")==null ? "" :mmMap.get("ordernumber").toString();
                    String mmyaopinmingcheng=mmMap.get("yaopinmingcheng")==null ? "" :mmMap.get("yaopinmingcheng").toString();
                    String mmGuige=mmMap.get("guige")==null ? "" :mmMap.get("guige").toString();
                    String mmDanwei=mmMap.get("danwei")==null ? "" :mmMap.get("danwei").toString();
                    String mmJiage=mmMap.get("jiage")==null ? "" :mmMap.get("jiage").toString();
                    String mmShengchanchangjia=mmMap.get("shengchanchangjia")==null ? "" :mmMap.get("shengchanchangjia").toString();
                    String mmZhixingshijian=mmMap.get("zhixingshijian")==null ? "" :mmMap.get("zhixingshijian").toString();
                    String mmYuanbenbu=mmMap.get("yuanbenbu")==null ? "" :mmMap.get("yuanbenbu").toString();
                    String mmQinhuai=mmMap.get("qinhuai")==null ? "" :mmMap.get("qinhuai").toString();
                    String mmresult=mmMap.get("result")==null ? "" :mmMap.get("result").toString();
                    String mmbiaojueren=mmMap.get("username")==null ? "" :mmMap.get("username").toString();

                    String mmyijian="";
                    if("1".equals(mmresult)){
                        mmyijian="同意";
                    }else if("0".equals(mmresult)){
                        mmyijian="不同意";
                    }else {
                        mmyijian="不同意";
                    }
                    wsheet.addCell(new Label(0, mmKaishihang,mmXuhao,wcfFC));
                    wsheet.addCell(new Label(1, mmKaishihang,mmyaopinmingcheng,wcfFC));
                    wsheet.addCell(new Label(2, mmKaishihang,mmGuige,wcfFC));
                    wsheet.addCell(new Label(3, mmKaishihang,mmDanwei,wcfFC));
                    wsheet.addCell(new Label(4, mmKaishihang,mmJiage,wcfFC));
                    wsheet.addCell(new Label(5, mmKaishihang,mmShengchanchangjia,wcfFC));
                    wsheet.addCell(new Label(6, mmKaishihang,mmZhixingshijian,wcfFC));
                    wsheet.addCell(new Label(7, mmKaishihang,mmYuanbenbu,wcfFC));
                    wsheet.addCell(new Label(8, mmKaishihang,mmQinhuai,wcfFC));
                    wsheet.addCell(new Label(9, mmKaishihang,mmyijian,wcfFC));
                    mmKaishihang = mmKaishihang+1;
                }
            }
            wbook.write(); // 写入文件
            wbook.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }finally{
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    public List<Map<String,Object>>getHuizongbiao(String ppXiangmuId){
        return ddMapper.getHuizongbiao(ppXiangmuId);
    }
}
